clear
// Load election dates
insheet using "$sourcedatadir/wtid/view_election.csv"
keep if country_name == "Sweden"
keep if election_type == "parliament"
rename country_name country
rename election_date election_date_str
gen election_date = date(election_date_str, "YMD")
format election_date %td
bys election_date: keep if _n == 1
keep country election_date
gen year = year(election_date)
save "$gendatadir/se_election_dates.dta", replace

clear
do "$scriptsdir/se_preparecdf.do"

clear
do "$scriptsdir/se_prepareincome.do"

clear
use "$gendatadir/swedenincomeprocessed.dta"
gen country = "Sweden"

tsset year


merge 1:1 year using "$gendatadir/se_election_dates.dta", nogen

merge 1:1 country year using "$gendatadir/wtid_processed.dta", keep(master match)
drop _merge

tsset year
sort year


// Generate growth rates for the various income groups
gen inc_m_quin1_scb_g1  = (inc_m_quin1_scb  - l.inc_m_quin1_scb)/l.inc_m_quin1_scb
gen inc_m_quin2_scb_g1  = (inc_m_quin2_scb  - l.inc_m_quin2_scb)/l.inc_m_quin2_scb
gen inc_m_quin3_scb_g1  = (inc_m_quin3_scb  - l.inc_m_quin3_scb)/l.inc_m_quin3_scb
gen inc_m_quin4_scb_g1  = (inc_m_quin4_scb  - l.inc_m_quin4_scb)/l.inc_m_quin4_scb
gen inc_m_quin12_scb_g1 = (inc_m_quin12_scb - l.inc_m_quin12_scb)/l.inc_m_quin12_scb
gen inc_ll_95_scb_g1    = (inc_ll_top5_scb  - l.inc_ll_top5_scb)/l.inc_ll_top5_scb
gen inc_m_top5_scb_g1   = (inc_m_top5_scb   - l.inc_m_top5_scb)/l.inc_m_top5_scb
gen inc_mean_scb_g1     = (inc_mean_scb     - l.inc_mean_scb)/l.inc_mean_scb

// Generate growth rates for the various income groups
gen inc_ul_quin1_yb_g1 = (inc_ul_quin1_yb - l.inc_ul_quin1_yb)/l.inc_ul_quin1_yb
gen inc_ul_quin2_yb_g1 = (inc_ul_quin2_yb - l.inc_ul_quin2_yb)/l.inc_ul_quin2_yb
gen inc_ul_quin3_yb_g1 = (inc_ul_quin3_yb - l.inc_ul_quin3_yb)/l.inc_ul_quin3_yb
gen inc_ul_quin4_yb_g1 = (inc_ul_quin4_yb - l.inc_ul_quin4_yb)/l.inc_ul_quin4_yb
gen inc_ll_95_yb_g1    = (inc_ll_95_yb - l.inc_ll_95_yb)/l.inc_ll_95_yb
*gen inc_mean_quin5_g1 = (inc_mean_quin5 - l.inc_mean_quin5)/l.inc_mean_quin5
gen inc_mean_yb_g1     = (inc_mean_yb - l.inc_mean_yb)/l.inc_mean_yb

// Create variables that use SCB growth where possible, but yearbook growth otherwise.
gen     inc_quin1_xx_g1  = inc_m_quin1_scb_g1
replace inc_quin1_xx_g1  = inc_ul_quin1_yb_g1 if inc_quin1_xx_g1 == .
gen     inc_quin2_xx_g1  = inc_m_quin2_scb_g1
replace inc_quin2_xx_g1  = inc_ul_quin2_yb_g1 if inc_quin2_xx_g1 == .
gen     inc_quin3_xx_g1  = inc_m_quin3_scb_g1
replace inc_quin3_xx_g1  = inc_ul_quin3_yb_g1 if inc_quin3_xx_g1 == .
gen     inc_quin4_xx_g1  = inc_m_quin4_scb_g1
replace inc_quin4_xx_g1  = inc_ul_quin4_yb_g1 if inc_quin4_xx_g1 == .
gen     inc_quin12_xx_g1 = inc_m_quin12_scb_g1
replace inc_quin12_xx_g1 = inc_ul_quin1_yb_g1 if inc_quin12_xx_g1 == .
gen     inc_ll_95_xx_g1  = inc_ll_95_scb_g1
replace inc_ll_95_xx_g1  = inc_ll_95_yb_g1 if inc_ll_95_xx_g1 == .
gen     inc_m_top5_xx_g1  = inc_m_top5_scb_g1
replace inc_m_top5_xx_g1  = inc_ll_95_yb_g1 if inc_m_top5_xx_g1 == .

gen     inc_mean_pr_g1 = (inc_mean_pr - l.inc_mean_pr)/l.inc_mean_pr
gen     inc_mean_di_g1 = (inc_mean_di - l.inc_mean_di)/l.inc_mean_di


// Use SCB-derived mean (based on accurate decile data) for our inc_mean measure, in preference
gen     inc_mean_xx_g1 = inc_mean_scb_g1
replace inc_mean_xx_g1 = inc_mean_yb_g1 if inc_mean_xx_g1 == .
gen     inc_mean_g1    = inc_mean_xx_g1


*tsline inc_mean_scb_g1 wtid_incgrowthmean inc_mean_yb_g1                     if year > 1965,	xtitle("Year") xlabel(1965(5)2015) graphregion(fcolor(white)) title("Mean income growth rates") legend(lab(1 "SCB") lab(2 "WTID") lab(3 "Yearbooks"))
tsline inc_mean_scb_g1 wtid_incgrowthmean                     if year > 1965,	xtitle("Year") xlabel(1965(5)2015) graphregion(fcolor(white)) title("Mean income growth rates") legend(lab(1 "SCB") lab(2 "WTID"))
graph export "$imagedir/se_tslines_mean.pdf", replace
window manage close graph
*tsline inc_m_top5_scb_g1 inc_ll_95_scb_g1 wtid_incgrowthtop5 inc_ll_95_yb_g1 if year > 1965,	xtitle("Year") xlabel(1965(5)2015) graphregion(fcolor(white)) title("Top income growth rates")  legend(lab(1 "SCB: T5M") lab(2 "SCB: P95") lab(3 "WTID: T5M") lab(4 "Yearbooks Dist.: P95"))
tsline inc_m_top5_scb_g1 inc_ll_95_scb_g1 wtid_incgrowthtop5 if year > 1965,	xtitle("Year") xlabel(1965(5)2015) graphregion(fcolor(white)) title("Top income growth rates")  legend(lab(1 "SCB: T5M") lab(2 "SCB: P95") lab(3 "WTID: T5M"))
graph export "$imagedir/se_tslines_t5.pdf", replace
window manage close graph
tsline top5meanerror pctile95thresholderror if year > 1975,										xtitle("Year") xlabel(1975(5)2015) graphregion(fcolor(white)) title("Top income level margins of error") legend(lab(1 "SCB: T5M") lab(2 "SCB: P95"))
graph export "$imagedir/se_tslines_t5error.pdf", replace
window manage close graph
gen top5meanerror_pc          = 100*top5meanerror/top5meanlevel
gen pctile95thresholderror_pc = 100*pctile95thresholderror/pctile95thresholdlevel
tsline top5meanerror_pc pctile95thresholderror_pc if year > 1975,								xtitle("Year") xlabel(1975(5)2015) graphregion(fcolor(white)) title("Top income level margins of error") legend(lab(1 "SCB: T5M") lab(2 "SCB: P95"))
graph export "$imagedir/se_tslines_t5errorpc.pdf", replace
window manage close graph

gen            share5_x_T5g1 =	share5 * inc_ll_95_xx_g1
label variable share5			"$ Share^{T5}_t $"
label variable share5_x_T5g1	"$ Share^{T5}_{t} \times Growth^{P95}_{t} $"


gen election_year_weight = doy(election_date)/365
foreach varn of varlist inc_* wtid_incgrowth* share* {
  gen orig_`varn' = `varn'
  replace `varn' = election_year_weight * orig_`varn' + (1-election_year_weight) * l.orig_`varn' if election_year_weight != .
  *replace `varn' = election_year_weight * orig_`varn' + (1-election_year_weight) * orig_`varn'[_n-1] if election_year_weight != .
  *replace `varn' = l.`varn' if halfyear(election_date) == 1
}



gen yr = year
*merge 1:m yr using "Pooled dataset/TH_pooling/sweden cdf.dta", nogenerate
merge 1:m yr using "$gendatadir/se_cdf.dta", nogenerate

gen logtenure = log(incumbent_tenure)


sort year

// Create weights that weight each election equally
gen naiveweight = 1
bys year: egen naiveweight_sum = total(naiveweight)
gen naiveweight_eqelec = 10000*naiveweight/naiveweight_sum 


label variable vote_ce 				"$ VoteIncumbent_{i,t} $"
label variable inc_mean_g1			"$ Growth^{M}_{t} $"
label variable incumbent_tenure		"$ Tenure_{t} $"
label variable logtenure			"$ \log Tenure_{t} $"
label variable pid_ce 				"$ ProPartyID_{i,t} $"
label variable inc_ll_95_xx_g1		"$ Growth^{P95}_{t} $"
label variable inc_quin1_xx_g1		"$ Growth^{Q1}_{t} $"
label variable inc_quin2_xx_g1		"$ Growth^{Q2}_{t} $"
label variable inc_quin3_xx_g1		"$ Growth^{Q3}_{t} $"
label variable inc_quin4_xx_g1		"$ Growth^{Q4}_{t} $"
label variable inc_quin12_xx_g1		"$ Growth^{Q1-2}_{t} $"
label variable wtid_incgrowthmean	"$ Growth^M_{t} $"
label variable wtid_incgrowthtop5	"$ Growth^{T5M}_{t} $"
label variable inc_m_top5_xx_g1		"$ Growth^{T5}_{t} $"

label variable inc_ul_quin1_yb_g1	"$ Growth^{P20}_{t} $"

label variable gpi					"$ PolInfo_{i,t} $"
label variable socref				"$ OpposeWelf_{i,t} $"
label variable degree				"$ University_{i,t} $"
label variable unioninfluenced		"$ UnionInfl_{i,t} $"


global estout_options          label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(ll bic r2_p N N_clust xtercsamp, fmt(0 0 2 0 0) labels("Log Likelihood" "BIC" "Pseudo $ R^2 $" "N" "N of elections" "Income tercile(s)")) cells( " b( fmt(2) ) p(fmt(2)) " ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )
global estout_options_w_se     label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(ll bic r2_p N N_clust xtercsamp, fmt(0 0 2 0 0)     labels("Log Likelihood" "BIC" "Pseudo $ R^2 $" "N" "N of elections" "Income tercile(s)")) cells( " b( fmt(2) ) se(fmt(2)) p(fmt(2))" ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )
global estout_options_ols_w_se label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(r2 N xtercsamp, fmt(2 0 0) labels("$ R^2 $" "N" "Income tercile(s)")) cells( " b( fmt(2) ) se(fmt(2)) p(fmt(2))" ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )


//// Estimate some aggregated models by ols - so excluding PID.
preserve
collapse (mean) vote_ce incumbent_tenure incumbent_sap logtenure pid_ce inc_mean_g1 inc_ll_95_xx_g1 share5 [pweight=naiveweight_eqelec], by(year)
reg vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_xx_g1
reg vote_ce logtenure inc_mean_g1 inc_ll_95_xx_g1
reg vote_ce logtenure inc_mean_g1 inc_ll_95_xx_g1 incumbent_sap
reg pid_ce logtenure inc_mean_g1 inc_ll_95_xx_g1
estadd local xtercsamp = "All"
estimates store PIDOLSAll
restore

preserve
collapse (mean) vote_ce incumbent_tenure incumbent_sap logtenure pid_ce inc_mean_g1 inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 0, by(year)
reg vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_xx_g1
reg vote_ce logtenure inc_mean_g1 inc_ll_95_xx_g1
reg vote_ce logtenure inc_mean_g1 inc_ll_95_xx_g1 incumbent_sap
reg pid_ce logtenure inc_mean_g1 inc_ll_95_xx_g1
estadd local xtercsamp = "Low"
estimates store PIDOLSLow
restore

preserve
collapse (mean) vote_ce incumbent_tenure incumbent_sap logtenure pid_ce inc_mean_g1 inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 1, by(year)
reg vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_xx_g1
reg vote_ce logtenure inc_mean_g1 inc_ll_95_xx_g1
reg vote_ce logtenure inc_mean_g1 inc_ll_95_xx_g1 incumbent_sap
reg pid_ce logtenure inc_mean_g1 inc_ll_95_xx_g1
estadd local xtercsamp = "Mid"
estimates store PIDOLSMid
restore

estout  PIDOLSAll PIDOLSLow PIDOLSMid		using "$texdir/se_pid.tex",	$estout_options_ols_w_se order(logtenure inc_mean_g1 inc_ll_95_xx_g1)


//// Section of models that we have decided to include in the main text of CBEVComparative

probit vote_ce logtenure        pid_ce inc_mean_g1 inc_ll_95_xx_g1 [pweight=naiveweight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenure
probit vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLow
probit vote_ce logtenure pid_ce inc_quin12_xx_g1   inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1Low
probit vote_ce logtenure pid_ce inc_mean_g1 inc_quin12_xx_g1 [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store MeanQ1Low
probit vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMid
probit vote_ce logtenure pid_ce inc_quin3_xx_g1   inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredQ3Mid
probit vote_ce logtenure pid_ce inc_mean_g1 inc_quin3_xx_g1 [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store MeanQ3Mid

estout  BartelsLogtenure PreferredLow PreferredQ1Low PreferredMid PreferredQ3Mid		using "$texdir/se_cbevcomp_tableA.tex",	$estout_options_w_se order(logtenure pid_ce inc_mean_g1 inc_quin12_xx_g1 inc_quin3_xx_g1 inc_ll_95_xx_g1)


// An identifier so we can summarize based on year not individual-level sample size
bys year: gen firstob = 1 if _n == 1
replace firstob = 0 if firstob == .


// Some code to generate predicted probabilities of incumbent vote to illustrate magnitudes in the paper.
estimates restore PreferredLow
summ inc_ll_95_xx_g1 if firstob == 1
margins, at(inc_ll_95_xx_g1=(0.018 0.058)) asobserved

estimates restore PreferredQ1Low
summ inc_quin12_xx_g1 if firstob == 1
margins, at(inc_quin12_xx_g1=(0.013 0.075)) asobserved
margins, at(inc_ll_95_xx_g1=(0.018 0.058)) asobserved

estimates restore PreferredMid
summ inc_ll_95_xx_g1 if firstob == 1
margins, at(inc_ll_95_xx_g1=(0.018 0.058)) asobserved

estimates restore PreferredQ3Mid
summ inc_quin3_xx_g1 if firstob == 1
margins, at(inc_quin3_xx_g1=(0.016 0.045)) asobserved





//// Section of models that we present in supplementary material

probit vote_ce incumbent_tenure        pid_ce inc_mean_g1 inc_ll_95_xx_g1 [pweight=naiveweight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store Bartels
probit vote_ce logtenure        pid_ce inc_mean_g1                 [pweight=naiveweight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenureNoT5
probit vote_ce logtenure        pid_ce             inc_ll_95_xx_g1 [pweight=naiveweight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenureNoMean
probit vote_ce logtenure pid_ce             inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMidNoMean
probit vote_ce logtenure pid_ce inc_mean_g1                 [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMidNoT5

estout  Bartels BartelsLogtenure		using "$texdir/se_cbevcomp_tableB.tex",	$estout_options_w_se order(incumbent_tenure logtenure pid_ce inc_mean_g1 inc_ll_95_xx_g1)
estout  BartelsLogtenureNoT5 BartelsLogtenureNoMean PreferredMidNoT5 PreferredMidNoMean		using "$texdir/se_cbevcomp_tableC.tex",	$estout_options_w_se order(logtenure pid_ce inc_mean_g1 inc_ll_95_xx_g1)


probit vote_ce logtenure         inc_mean_g1 inc_ll_95_xx_g1 [pweight=naiveweight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenureNoPID
probit vote_ce logtenure  inc_mean_g1 inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLowNoPID
probit vote_ce logtenure  inc_quin12_xx_g1   inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1LowNoPID
probit vote_ce logtenure  inc_mean_g1 inc_quin12_xx_g1 [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store MeanQ1LowNoPID
probit vote_ce logtenure  inc_mean_g1 inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMidNoPID
probit vote_ce logtenure  inc_quin3_xx_g1   inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredQ3MidNoPID
probit vote_ce logtenure  inc_mean_g1 inc_quin3_xx_g1 [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store MeanQ3MidNoPID

*estout  BartelsLogtenure PreferredLow PreferredQ1Low MeanQ1Low PreferredMid PreferredQ3Mid MeanQ3Mid		using "$texdir/se_cbevcomp_tableA.tex",	$estout_options order(logtenure pid_ce inc_mean_g1 inc_quin1_xx_g1 inc_quin3_xx_g1 inc_ll_95_xx_g1)
estout  BartelsLogtenureNoPID PreferredLowNoPID PreferredQ1LowNoPID PreferredMidNoPID PreferredQ3MidNoPID		using "$texdir/se_cbevcomp_tableD.tex",	$estout_options_w_se order(logtenure inc_mean_g1 inc_quin12_xx_g1 inc_quin3_xx_g1 inc_ll_95_xx_g1)


probit vote_ce logtenure pid_ce inc_quin12_xx_g1  inc_mean_g1 inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store OwnMeanLow
probit vote_ce logtenure pid_ce inc_quin3_xx_g1  inc_mean_g1 inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store OwnMeanMid
probit vote_ce logtenure pid_ce inc_quin12_xx_g1 inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ12Low

estout  OwnMeanLow OwnMeanMid PreferredQ12Low		using "$texdir/se_cbevcomp_tableE.tex",	$estout_options_w_se order(logtenure pid_ce inc_mean_g1 inc_quin12_xx_g1 inc_quin3_xx_g1 inc_ll_95_xx_g1)


probit vote_ce logtenure        pid_ce inc_mean_g1 inc_ll_95_xx_g1 share5 share5_x_T5g1 [pweight=naiveweight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store BartelsLogtenureS5
probit vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_xx_g1 share5 share5_x_T5g1 [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredLowS5
probit vote_ce logtenure pid_ce inc_mean_g1 inc_ll_95_xx_g1 share5 share5_x_T5g1 [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store PreferredMidS5

estout  BartelsLogtenureS5 PreferredLowS5 PreferredMidS5		using "$texdir/se_cbevcomp_tableF.tex",	$estout_options_w_se order(logtenure pid_ce inc_mean_g1 inc_ll_95_xx_g1 share5 share5_x_T5g1)


probit vote_ce logtenure pid_ce wtid_incgrowthmean wtid_incgrowthtop5 [pweight=naiveweight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store WTIDBartelsLogtenure
probit vote_ce logtenure pid_ce wtid_incgrowthmean wtid_incgrowthtop5 [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store WTIDPreferredLow
probit vote_ce logtenure pid_ce inc_quin12_xx_g1    wtid_incgrowthtop5 [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store WTIDPreferredQ1Low
probit vote_ce logtenure pid_ce wtid_incgrowthmean inc_quin12_xx_g1    [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store WTIDMeanQ1Low
probit vote_ce logtenure pid_ce wtid_incgrowthmean wtid_incgrowthtop5 [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store WTIDPreferredMid
probit vote_ce logtenure pid_ce inc_quin3_xx_g1    wtid_incgrowthtop5 [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store WTIDPreferredQ3Mid
probit vote_ce logtenure pid_ce wtid_incgrowthmean inc_quin3_xx_g1    [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store WTIDMeanQ3Mid

estout  WTIDBartelsLogtenure WTIDPreferredLow WTIDPreferredQ1Low WTIDPreferredMid WTIDPreferredQ3Mid		using "$texdir/se_cbevcomp_tableG.tex",	$estout_options_w_se order(logtenure pid_ce wtid_incgrowthmean inc_quin12_xx_g1 inc_quin3_xx_g1 wtid_incgrowthtop5)


probit vote_ce logtenure pid_ce inc_mean_g1     inc_m_top5_xx_g1 [pweight=naiveweight_eqelec], vce(cluster year)
estadd local xtercsamp = "All"
estimates store T5MBartelsLogtenure
probit vote_ce logtenure pid_ce inc_mean_g1     inc_m_top5_xx_g1 [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store T5MPreferredLow
probit vote_ce logtenure pid_ce inc_quin12_xx_g1 inc_m_top5_xx_g1 [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store T5MPreferredQ1Low
probit vote_ce logtenure pid_ce inc_mean_g1     inc_m_top5_xx_g1 [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store T5MPreferredMid
probit vote_ce logtenure pid_ce inc_quin3_xx_g1 inc_m_top5_xx_g1 [pweight=naiveweight_eqelec] if income3 == 1, vce(cluster year)
estadd local xtercsamp = "Mid"
estimates store T5MPreferredQ3Mid

estout  T5MBartelsLogtenure T5MPreferredLow T5MPreferredQ1Low T5MPreferredMid T5MPreferredQ3Mid		using "$texdir/se_cbevcomp_tableH.tex",	$estout_options_w_se order(logtenure pid_ce inc_mean_g1 inc_quin12_xx_g1 inc_quin3_xx_g1 inc_m_top5_xx_g1)



probit vote_ce logtenure pid_ce inc_ul_quin1_yb_g1   inc_ll_95_xx_g1 [pweight=naiveweight_eqelec] if income3 == 0, vce(cluster year)
estadd local xtercsamp = "Low"
estimates store PreferredQ1Low

estout  PreferredQ1Low		using "$texdir/se_cbevcomp_tableI.tex",	$estout_options_w_se order(logtenure pid_ce inc_ul_quin1_yb_g1 inc_ll_95_xx_g1)



//// Scatter plot of mean and t5 growth rates
bysort year: gen macro_analysis = 1 if _n == 1
bysort year: gen election_year = 1 if _N > 1
replace election_year = 0 if election_year == .
gen inc_ll_95_xx_g1_100 =  inc_ll_95_xx_g1 * 100
gen inc_mean_g1_100 = inc_mean_g1 * 100
twoway (function y=x, range(-10 10)) (scatter inc_ll_95_xx_g1_100 inc_mean_g1_100 if macro_analysis == 1 & election_year == 0, msymbol(oh) yline(0, lstyle(dot)) xline(0, lstyle(dot))) (scatter inc_ll_95_xx_g1_100 inc_mean_g1_100 if macro_analysis == 1 & election_year == 1, msymbol(o) mcolor(gs4)), scheme(s1mono) xtitle("Mean income growth (%)") ytitle("Top-5% income growth (%)") xlabel(-10(4)10) ylabel(-10(4)10) legend(off)
graph export "$imagedir/se_scatter_mean_t5.pdf", replace
window manage close graph

cor inc_ll_95_xx_g1_100 inc_mean_g1_100 if macro_analysis == 1
local rho =  string(r(rho), "%9.2fc")
capture {
  file open  rhofile using "$texdir/se_rho_mean_t5.tex", write replace
  file write rhofile "`rho'"
  file close rhofile
}
cor inc_ll_95_xx_g1_100 inc_mean_g1_100 if macro_analysis == 1 & election_year == 1
local rho =  string(r(rho), "%9.2fc")
capture {
  file open  rhofile using "$texdir/se_rho_mean_t5_eyears.tex", write replace
  file write rhofile "`rho'"
  file close rhofile
}


gen model_country = "Sweden"
gen model_name = ""
gen predicted_diff_mean_m = .
gen predicted_diff_mean_se = .
gen predicted_diff_t5_m = .
gen predicted_diff_t5_se = .
local counter = 1
foreach model in BartelsLogtenure PreferredLow PreferredQ1Low PreferredMid PreferredQ3Mid {
  estimates restore `model'
  summ inc_ll_95_xx_g1 if e(sample)
  local t5_mean =  r(mean)
  local t5_sd   =  r(sd)
  local t5_mean_sd = `t5_mean' + `t5_sd'
  margins, pwcompare at((asobserved) _all inc_ll_95_xx_g1 =(`t5_mean' `t5_mean_sd'))
  matrix pdiff_m = r(b_vs)
  matrix pdiff_v = r(V_vs)
  scalar pdiff_m = pdiff_m[1,1]
  scalar pdiff_se = pdiff_v[1,1]^(1/2)
  replace model_name = "`model'" if _n == `counter'
  replace predicted_diff_t5_m = pdiff_m if _n == `counter'
  replace predicted_diff_t5_se = pdiff_se if _n == `counter'
  // Now mean version
  if "`model'" == "BartelsLogtenure" | "`model'" == "PreferredLow" | "`model'" == "PreferredMid" {
    summ inc_mean_g1 if e(sample)
    local mean_mean =  r(mean)
    local mean_sd   =  r(sd)
    local mean_mean_sd = `mean_mean' + `t5_sd'
    margins, pwcompare at((asobserved) _all inc_mean_g1 =(`mean_mean' `mean_mean_sd'))
    matrix pdiff_m = r(b_vs)
    matrix pdiff_v = r(V_vs)
    scalar pdiff_m = pdiff_m[1,1]
    scalar pdiff_se = pdiff_v[1,1]^(1/2)
    replace model_name = "`model'" if _n == `counter'
    replace predicted_diff_mean_m = pdiff_m if _n == `counter'
    replace predicted_diff_mean_se = pdiff_se if _n == `counter'
  }
  // Increment counter
  local counter = `counter' + 1
}
keep model_country model_name predicted_diff_*
drop if model_name == ""
save "$gendatadir/se_predicted_differences.dta", replace

